﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Phidgets;
using System.Windows.Forms;
using Phidgets.Events;
using CardEngine.CGService;

namespace CardEngine
{
    class Game
    {
        private static class GameEngine
        {
            public static frmPlayer frmPlayer1 { get; set; }

            public static void run()
            {
                #region RFID Readers
                try
                {
                    RFID rfidPlayer1 = new RFID();
                    RFID rfidTable = new RFID();

                    //Initialization
                    CGService.CardGameServiceClient client = new CardGameServiceClient();
                    client.ShuffleDeck();
                    client.Close();
                    client = null;

                    rfidPlayer1.Attach += new AttachEventHandler(rfidPlayer1_Attach);
                    rfidPlayer1.Detach += new DetachEventHandler(rfidPlayer1_Detach);
                    rfidPlayer1.Error += new ErrorEventHandler(rfidPlayer1_Error);

                    rfidPlayer1.Tag += new TagEventHandler(rfidPlayer1_Tag);
                    rfidPlayer1.TagLost += new TagEventHandler(rfidPlayer1_TagLost);
                    rfidPlayer1.open();

                    //Initialization
                    rfidTable.Attach += new AttachEventHandler(rfidTable_Attach);
                    rfidTable.Detach += new DetachEventHandler(rfidTable_Detach);
                    rfidTable.Error += new ErrorEventHandler(rfidTable_Error);

                    rfidTable.Tag += new TagEventHandler(rfidTable_Tag);
                    rfidTable.TagLost += new TagEventHandler(rfidTable_TagLost);
                    rfidTable.open();

                    //Wait for a Phidget RFID to be attached before doing anything with 
                    //the object
                    Console.WriteLine("waiting for player attachment...");
                    rfidPlayer1.waitForAttachment();

                    rfidPlayer1.Antenna = true;
                    rfidPlayer1.LED = false;

                    Console.WriteLine("waiting for player attachment...");
                    rfidTable.waitForAttachment();

                    rfidTable.Antenna = true;
                    rfidTable.LED = true;

                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    if (null == frmPlayer1)
                    {
                        frmPlayer1 = new frmPlayer();
                    }
                    Application.Run(frmPlayer1);


                    Console.WriteLine("Press any key to end...");
                    Console.Read();

                    //turn off the led
                    rfidPlayer1.LED = false;

                    //close the phidget and dispose of the object
                    rfidPlayer1.close();
                    rfidPlayer1 = null;

                    rfidTable.close();
                    rfidTable = null;

                    Console.WriteLine("Done.");
                }
                catch (PhidgetException ex)
                {
                    Console.WriteLine(ex.Description);
                }

                #endregion         
            }

            #region "Player 1 RFID Events"
            //attach event handler...display the serial number of the attached RFID phidget
            static void rfidPlayer1_Attach(object sender, AttachEventArgs e)
            {
                Console.WriteLine("RFIDReader {0} attached!",
                                        e.Device.SerialNumber.ToString());
            }

            //detach event handler...display the serial number of the detached RFID phidget
            static void rfidPlayer1_Detach(object sender, DetachEventArgs e)
            {
                Console.WriteLine("RFID reader {0} detached!",
                                        e.Device.SerialNumber.ToString());
            }

            //Error event handler...display the error description string
            static void rfidPlayer1_Error(object sender, ErrorEventArgs e)
            {
                Console.WriteLine(e.Description);
            }

            //Print the tag code of the scanned tag
            static void rfidPlayer1_Tag(object sender, TagEventArgs e)
            {
                RFID rfidPlayer1 = (RFID)sender;
                Console.WriteLine("Reader:" + rfidPlayer1.SerialNumber);
                Console.WriteLine("Tag {0} scanned", e.Tag);
                CardGameServiceClient client = new CardGameServiceClient();
                PlayerCard pc = client.GetPlayerCardByRFID(e.Tag);
                Console.WriteLine("Player 1 received a " + pc.Type.Trim() + " of " + pc.Suit.Trim() + ".");

                HeldBy hb = new HeldBy();
                hb.CardID = pc.CardID;
                Player[] ps = client.GetPlayers();


                var q = from p in ps
                        where p.PlayerName.ToLower().Trim().Equals("player 1")
                        select p;

                hb.PlayerID = q.First().PlayerID;
                PlayerCards result = client.TransferCardOwnership(hb);
                frmPlayer1.redraw();
                //frmPlayer1.setCardImage(frmPlayer.PlayArea.Player, 1, result.ImageFileName);


                client.Close();
                client = null;
            }

            //print the tag code for the tag that was just lost
            static void rfidPlayer1_TagLost(object sender, TagEventArgs e)
            {
                Console.WriteLine("Tag {0} lost", e.Tag);
            }

            #endregion

            #region "Table RFID Events"
            //attach event handler...display the serial number of the attached RFID phidget
            static void rfidTable_Attach(object sender, AttachEventArgs e)
            {
                Console.WriteLine("RFIDReader {0} attached!",
                                        e.Device.SerialNumber.ToString());
            }

            //detach event handler...display the serial number of the detached RFID phidget
            static void rfidTable_Detach(object sender, DetachEventArgs e)
            {
                Console.WriteLine("RFID reader {0} detached!",
                                        e.Device.SerialNumber.ToString());
            }

            //Error event handler...display the error description string
            static void rfidTable_Error(object sender, ErrorEventArgs e)
            {
                Console.WriteLine(e.Description);
            }

            //Print the tag code of the scanned tag
            static void rfidTable_Tag(object sender, TagEventArgs e)
            {
                RFID rfidTable = (RFID)sender;
                Console.WriteLine("Reader:" + rfidTable.SerialNumber);
                Console.WriteLine("Tag {0} scanned", e.Tag);
                CardGameServiceClient client = new CardGameServiceClient();
                PlayerCard pc = client.GetPlayerCardByRFID(e.Tag);
                Console.WriteLine("Player 1 discarded a " + pc.Type.Trim() + " of " + pc.Suit.Trim() + ".");

                HeldBy hb = new HeldBy();
                hb.CardID = pc.CardID;
                Player[] ps = client.GetPlayers();


                var q = from p in ps
                        where p.PlayerName.ToLower().Trim().Equals("table")
                        select p;

                hb.PlayerID = q.First().PlayerID;
                PlayerCards result = client.TransferCardOwnership(hb);

                frmPlayer1.redraw();
                //Display On-Screen Hand.
                
                
                client.Close();
                client = null;
            }

            //print the tag code for the tag that was just lost
            static void rfidTable_TagLost(object sender, TagEventArgs e)
            {
                Console.WriteLine("Tag {0} lost", e.Tag);
            }

            #endregion
        }

        [STAThread]
        static void Main(string[] args)
        {
            GameEngine.run();
        }
    }
}
